package com.ccb.ha.zhxf.controller;


import com.ccb.ha.zhxf.entity.Mechanism;
import com.ccb.ha.zhxf.result.Result;
import com.ccb.ha.zhxf.result.ResultEnum;
import com.ccb.ha.zhxf.result.ResultUtil;
import com.ccb.ha.zhxf.service.MechanismSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author GanYuzhou
 * @Date 2020/5/18 18:03
 * @Version 1.0
 */

@RestController
@RequestMapping(value = "/mechanism")
public class MechanismController {
    @Autowired
    private MechanismSerivce mechanismSerivce;
    private int code;
    private String msg;

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ResponseBody
    public Result findAll (){

        List<Mechanism> mechanisms = mechanismSerivce.findAll();

        if (mechanisms != null){
            return ResultUtil.success(mechanisms);
        }else {

            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(), ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @RequestMapping(value = "/findById", method = RequestMethod.POST)
    @ResponseBody
    public Result findById(int mechanism_id){

        List<Mechanism> mechanisms = mechanismSerivce.findById(mechanism_id);

        if (mechanisms !=null){
            return ResultUtil.success(mechanisms);
        }else {

            return ResultUtil.error(ResultEnum.DATA_IS_NULL.getCode(), ResultEnum.DATA_IS_NULL.getMsg());
        }
    }

    @RequestMapping(value = "/insertMechanism", method = RequestMethod.POST)
    @ResponseBody
    public Result inserMechanism(@RequestParam("mechanism_id") int mechanism_id,
                                 @RequestParam("mechanism_name") String mechanism_name) {
        Mechanism mechanisms = new Mechanism();
        mechanisms.setMechanism_id(mechanism_id);
        mechanisms.setMechanism_name(mechanism_name);
        int result = mechanismSerivce.insertMechanism(mechanisms);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }


    @RequestMapping(value = "/updateMechanism", method = RequestMethod.POST)
    @ResponseBody
    public Result updateMechanism(@RequestParam("mechanism_id") int mechanism_id,
                             @RequestParam("mechanism_name") String mechanism_name){
        Mechanism mechanisms = new Mechanism();
        mechanisms.setMechanism_id(mechanism_id);
        mechanisms.setMechanism_name(mechanism_name);
        int result = mechanismSerivce.updateMechanism(mechanisms);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }

    @RequestMapping(value = "/deleteMechanism", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteMechanism(@RequestParam("mechanism_id") int mechanism_id){

        int result = mechanismSerivce.deleteMechanism(mechanism_id);
        if (result == 1){

            return ResultUtil.success(result);
        }else {

            return ResultUtil.error(ResultEnum.USER_IS_EXISTS.getCode(), ResultEnum.USER_IS_EXISTS.getMsg());
        }
    }

}
